home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_005 / layers / layers.c < prev   
C/C++ Source or Header  |  1992-05-06  |  7KB  |  232 lines

  1. /*
  2.  *
  3.  *    DISCLAIMER:
  4.  *
  5.  *    This program is provided as a service to the programmer
  6.  *    community to demonstrate one or more features of the Amiga
  7.  *    personal computer.  These code samples may be freely used
  8.  *    for commercial or noncommercial purposes.
  9.  * 
  10.  *     Commodore Electronics, Ltd ("Commodore") makes no
  11.  *    warranties, either expressed or implied, with respect
  12.  *    to the program described herein, its quality, performance,
  13.  *    merchantability, or fitness for any particular purpose.
  14.  *    This program is provided "as is" and the entire risk
  15.  *    as to its quality and performance is with the user.
  16.  *    Should the program prove defective following its
  17.  *    purchase, the user (and not the creator of the program,
  18.  *    Commodore, their distributors or their retailers)
  19.  *    assumes the entire cost of all necessary damages.  In 
  20.  *    no event will Commodore be liable for direct, indirect,
  21.  *    incidental or consequential damages resulting from any
  22.  *    defect in the program even if it has been advised of the 
  23.  *    possibility of such damages.  Some laws do not allow
  24.  *    the exclusion or limitation of implied warranties or
  25.  *    liabilities for incidental or consequential damages,
  26.  *    so the above limitation or exclusion may not apply.
  27.  *
  28.  */
  29.  
  30. /* layers.c */
  31.  
  32. /* **********************************************************************
  33.  * THIS EXAMPLE SHOWS HOW TO USE THE layers.library.  Certain functions
  34.  * are not available in the system software prior to the release of
  35.  * version 1.1.  Therefore this example can only be compiled if your
  36.  * C-disk supports version 1.1 or beyond.
  37.  ********************************************************************* */
  38.  
  39. /* author:  Rob Peck, 12/1/85 */
  40.  
  41. /* This code may be freely utilized to develop programs for the Amiga. */
  42.  
  43.  
  44. #include "exec/types.h"
  45. #include "graphics/gfx.h"
  46. #include "hardware/dmabits.h"
  47. #include "hardware/custom.h"
  48. #include "hardware/blit.h"
  49. #include "graphics/gfxmacros.h"
  50. #include "graphics/copper.h"
  51. #include "graphics/view.h"
  52. #include "graphics/gels.h"
  53. #include "graphics/regions.h"
  54. #include "graphics/clip.h"
  55. #include "exec/exec.h"
  56. #include "graphics/text.h"
  57. #include "graphics/gfxbase.h"
  58. /* ********************** added for layers support ************************ */
  59. #include "graphics/layers.h"
  60. #include "graphics/clip.h"
  61. /* ********************** added for layers support ************************ */
  62.  
  63.  
  64. #define DEPTH 2  
  65. #define WIDTH 320 
  66. #define HEIGHT 200 
  67. #define NOT_ENOUGH_MEMORY -1000
  68. #define FOREVER for(;;) 
  69.         /* construct a simple display */ 
  70.  
  71. #define FLAGS LAYERSMART
  72. struct View *oldview;    /* save pointer to old view so can go back to sys */ 
  73. struct View v;
  74. struct ViewPort vp;
  75. struct ColorMap *cm;    /* pointer to colormap structure, dynamic alloc */
  76. struct RasInfo ri;
  77. struct BitMap b;
  78. /* made 3 separate rastports for layers testing ********************** */
  79. struct RastPort *rp[3];        /* rastport for each layer */
  80. /* dynamically created RastPorts from the calls to CreateUpfrontLayer */
  81.  
  82. short i,j,k,n;
  83. struct ColorMap *GetColorMap();
  84. struct GfxBase *GfxBase;
  85.  
  86. SHORT  boxoffsets[] = { 802, 2010, 3218 };
  87. USHORT colortable[] = { 0x000, 0xf00, 0x0f0, 0x00f };
  88.             /* black, red, green, blue */
  89. UBYTE *displaymem;
  90. UBYTE *colorpalette;
  91.  
  92. long LayersBase;
  93. struct Layer_Info *li;
  94. struct Layer *layer[3];
  95. extern struct Layer *CreateUpfrontLayer();
  96. extern struct Layer_Info *NewLayerInfo();
  97.  
  98. main()
  99. {
  100.     GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0);
  101.     if (GfxBase == NULL) exit(1);
  102.  
  103.     LayersBase = OpenLibrary("layers.library",0); 
  104.     if(LayersBase == NULL) exit(2);
  105.  
  106.     oldview = GfxBase->ActiView;    /* save current view, go back later */
  107.         /* example steals screen from Intuition if started from WBench */
  108.     
  109.     li = NewLayerInfo();    /* get a LayerInfo structure */
  110.     if(li == NULL) exit(100);
  111.  
  112.     /* not needed if gotten by NewLayerInfo     InitLayers(li);   */
  113.  
  114.                             /* initialize view */
  115.         InitView(&v);
  116.                 /* link view into viewport */
  117.         v.ViewPort = &vp;
  118.                 /* init view port */
  119.         InitVPort(&vp);
  120.                 /* now specify critical characteristics */
  121.     vp.DWidth = WIDTH;
  122.     vp.DHeight = HEIGHT;
  123.     vp.RasInfo = &ri;
  124.                 /* init bit map (for rasinfo and rastport) */
  125.         InitBitMap(&b,DEPTH,WIDTH,HEIGHT);
  126.                 /* (init RasInfo) */
  127.         ri.BitMap = &b;
  128.         ri.RxOffset = 0;    /* align upper left corners of display
  129.                  * with upper left corner of drawing area */
  130.         ri.RyOffset = 0;
  131.     ri.Next = NULL;
  132.                 /* (init color table) */
  133.     cm = GetColorMap(4);    /* 4 entries, since only 2 planes deep */
  134.     colorpalette = (UBYTE *)cm->ColorTable;
  135.     for(i=0; i<4; i++)
  136.         *colorpalette++ = colortable[i];
  137.                 /* copy my colors into this data structure */
  138.     vp.ColorMap = cm;    /* link it with the viewport */
  139.  
  140.                                 /* allocate space for bitmap */
  141.         for(i=0; i<DEPTH; i++)
  142.            {
  143.            b.Planes[i] = (PLANEPTR)AllocRaster(WIDTH,HEIGHT);
  144.            if(b.Planes[i] == NULL) exit(NOT_ENOUGH_MEMORY);
  145.            }
  146.  
  147.     MakeVPort( &v, &vp );    /* construct copper instr (prelim) list */
  148.     MrgCop( &v );        /* merge prelim lists together into a real 
  149.                  * copper list in the view structure. */
  150.  
  151.     for(i=0; i<2; i++)
  152.         {
  153.         displaymem = (UBYTE *)b.Planes[i];
  154.         for(j=0; j<RASSIZE(WIDTH,HEIGHT); j++)
  155.             *displaymem++ = 0;    
  156.         /* zeros to all bytes of the display area */                    }
  157.  
  158.     LoadView(&v);
  159.  
  160.     /* now fill some boxes so that user can see something */
  161.  
  162.     layer[0] = CreateUpfrontLayer(li,&b,5,5,85,65,FLAGS,NULL);
  163.         /* layerinfo, common bitmap, x,y,x2,y2,
  164.          * flags = 0 (simple refresh), null pointer to superbitmap */
  165.     if(layer[0] == NULL) goto cleanup1;
  166.     
  167.     layer[1] = CreateUpfrontLayer(li,&b,20,20,100,80,FLAGS,NULL);
  168.     if(layer[1] == NULL) goto cleanup2;
  169.  
  170.     layer[2] = CreateUpfrontLayer(li,&b,45,45,125,105,FLAGS,NULL);
  171.     if(layer[2] == NULL) goto cleanup3;
  172.  
  173.     for(i=0; i<3; i++)    /* layers are created, now draw to them */
  174.     { 
  175.     rp[i] = layer[i]->rp;
  176.     SetAPen(rp[i],i+1);
  177.     SetDrMd(rp[i],JAM1);
  178.     RectFill(rp[i],0,0,79,59);
  179.     }
  180.     SetAPen(rp[0],0);
  181.     Move(rp[0],5,30);
  182.     Text(rp[0],"Layer 0",7);
  183.  
  184.     SetAPen(rp[1],0);
  185.     Move(rp[1],5,30);
  186.     Text(rp[1],"Layer 1",7);
  187.  
  188.     SetAPen(rp[2],0);
  189.     Move(rp[2],5,30);
  190.     Text(rp[2],"Layer 2",7);
  191.     
  192.     Delay(120);    /* 2 seconds before first change */
  193.     BehindLayer(li,layer[2]);
  194.  
  195.     Delay(120);    /* another change 2 seconds later */
  196.  
  197.     UpfrontLayer(li,layer[0]);
  198.  
  199.     for(i=0; i<30; i++)
  200.     {
  201.         MoveLayer(li,layer[1],1,3);
  202.         Delay(10);    /* wait .16 seconds (uses DOS function) */
  203.         
  204.     }
  205.  
  206.     cleanup3:
  207.     LoadView(oldview);              /* put back the old view  */
  208.     DeleteLayer(li,layer[2]);
  209.     cleanup2:
  210.     DeleteLayer(li,layer[1]);
  211.     cleanup1:
  212.     DeleteLayer(li,layer[0]);
  213.  
  214.     DisposeLayerInfo(li);
  215.     FreeMemory();    
  216.     CloseLibrary(GfxBase);
  217.  
  218. }    /* end of main() */
  219.  
  220.  
  221. FreeMemory()
  222. {              /* return user and system-allocated memory to sys manager */
  223.  
  224.       for(i=0; i<DEPTH; i++)            /* free the drawing area */
  225.            FreeRaster(b.Planes[i],WIDTH,HEIGHT);
  226.     FreeColorMap(cm);            /* free the color map */
  227.         /* free dynamically created structures */
  228.     FreeVPortCopLists(&vp);        
  229.      FreeCprList(v.LOFCprList);   
  230.     return(0);
  231. }    
  232.